import QtQuick 2.15
import QtQuick.Controls 2.15

// 验证码组件
// 功能：随机生成字母/数字验证码，支持点状背景，双击刷新
Item {
    id: root
    
    width: 300
    height: 90
    
    property int codeNum: 4  // 验证码字符数量
    property string verificationCode: ""  // 当前验证码
    property int backgroundStyle: 0  // 0: E_NONE, 1: E_DOT
    
    // 颜色数组（对应原始C++的Qt::GlobalColor）
    property var colors: []
    
    // 初始化随机数种子
    Component.onCompleted: {
        generateCode()
    }
    
    // 生成随机验证码
    function generateCode() {
        var destCode = ""
        for (var i = 0; i < codeNum; i++) {
            var flag = Math.floor(Math.random() * 2)
            if (flag === 0) {
                // 生成数字 0-9
                var c = '0'.charCodeAt(0) + Math.floor(Math.random() * 10)
                destCode += String.fromCharCode(c)
            } else {
                // 生成字母 a-z 或 A-Z
                var base = (Math.floor(Math.random() * 2) === 0) ? 'a'.charCodeAt(0) : 'A'.charCodeAt(0)
                destCode += String.fromCharCode(base + Math.floor(Math.random() * 26))
            }
        }
        verificationCode = destCode
        
        // 生成随机颜色（对应原始C++的Qt::GlobalColor 2-17）
        var newColors = []
        for (var j = 0; j < codeNum; j++) {
            // Qt::GlobalColor 2-17 对应：white, black, red, darkRed, green, darkGreen, blue, darkBlue, cyan, darkCyan, magenta, darkMagenta, yellow, darkYellow, gray, darkGray, lightGray
            var colorIndex = 2 + Math.floor(Math.random() * 16)
            newColors.push(getColorByIndex(colorIndex))
        }
        colors = newColors
        
        // 更新点状背景
        if (backgroundStyle === 1) {
            updateDotBackground()
        }
    }
    
    // 根据索引获取颜色（对应Qt::GlobalColor）
    function getColorByIndex(index) {
        var colorMap = {
            2: "#FFFFFF",   // white
            3: "#000000",   // black
            4: "#FF0000",   // red
            5: "#800000",   // darkRed
            6: "#00FF00",   // green
            7: "#008000",   // darkGreen
            8: "#0000FF",   // blue
            9: "#000080",   // darkBlue
            10: "#00FFFF",  // cyan
            11: "#008080",  // darkCyan
            12: "#FF00FF",  // magenta
            13: "#800080",  // darkMagenta
            14: "#FFFF00",  // yellow
            15: "#808000",  // darkYellow
            16: "#808080",  // gray
            17: "#404040"   // darkGray
        }
        return colorMap[index] || "#000000"
    }
    
    // 更新点状背景
    function updateDotBackground() {
        // 清除现有点并创建新的点
        dotModel.clear()
        for (var j = 0; j < 300; j++) {
            dotModel.append({
                "x": Math.floor(Math.random() * (root.width - 1)),
                "y": Math.floor(Math.random() * (root.height - 1)),
                "colorIndex": j % codeNum
            })
        }
    }
    
    // 背景矩形
    Rectangle {
        anchors.fill: parent
        color: "#FFFEF0"  // 对应原始C++的QColor(255, 250, 240)
        
        // 点状背景
        Repeater {
            id: dotRepeater
            model: ListModel {
                id: dotModel
            }
            
            Rectangle {
                x: model.x
                y: model.y
                width: 2
                height: 2
                color: root.colors[model.colorIndex] || "#000000"
                visible: root.backgroundStyle === 1
            }
        }
        
        // 验证码文字
        Row {
            anchors.fill: parent
            spacing: 0
            
            Repeater {
                model: root.codeNum
                
                Text {
                    width: root.width / root.codeNum
                    height: root.height
                    text: root.verificationCode[index] || ""
                    font.family: "Comic Sans MS"
                    font.pixelSize: 36
                    color: root.colors[index] || "#000000"
                    horizontalAlignment: Text.AlignHCenter
                    verticalAlignment: Text.AlignVCenter
                }
            }
        }
    }
    
    // 双击刷新验证码
    MouseArea {
        anchors.fill: parent
        onDoubleClicked: {
            generateCode()
        }
    }
    
    // 设置背景样式
    function setBackgroundStyle(style) {
        backgroundStyle = style
        if (style === 1) {
            updateDotBackground()
        }
    }
    
    // 获取当前验证码
    function getVerificationCode() {
        return verificationCode
    }
}

